﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingWorkshop.BLL;
using TrainingWorkshop.Model;
using System.Configuration;


namespace Presentation
{
 
    public partial class Teacher_List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Logical log = new Logical();
                if (log.OperationChoose("Select", true))
                    Bind(GridView1, log.Re);
                else
                    Response.Write("Database is Empty");


            }
        }

        private void Bind(GridView GV, List<Teacher> table)
        {
            GV.DataSource = table;
            GV.DataBind();
        }

        protected void Button_Create_Click(object sender, EventArgs e)//call create or update page
        {
            Response.Redirect("Create.aspx");//.............................................................................problem
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)//call update and delete 
        {
            if ("Update" == e.CommandName)
            {
                string hid = e.CommandArgument.ToString();
                Response.Redirect("Update.aspx?id=" + hid); 
            }

            if ("DeleteRow" == e.CommandName)
            {
                string hid = e.CommandArgument.ToString();
                int teacherid = Convert.ToInt32(hid);
                Logical log = new Logical();
                log.ChooseTable("Teacher", false, teacherid);
                foreach (var a in log.Tea)
                {
                    Logical l =new Logical();
                    l.ChooseTable("Department",false,a.DepartMent.DepartmentId);
                    Department myDep = new Department();
                    foreach (var p in l.Dep)
                    {
                        myDep = p;
                    }
                    

                    Teacher temp =new Teacher
                    {
                         TeacherId = a.TeacherId,
                         TeacherName = a.TeacherName,
                         TeacherGender = a.TeacherGender,
                         DepartMent = myDep
                    };
                  
                    if (log.OperationChoose("Delete", false, temp))
                        Response.Write("<script>alert('Delete Succeed');window.location.href='TeacherList.aspx';</script>");
                    else
                        Response.Write("<script>alert('Delete Failed');windows.location.href='TeacherList.aspx';</script>");
                    Response.End();
                }
               
            }
        }
    }
}